﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Client.Helper;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InfoSys
{
    public partial class SysAccountEditForm : BaseForm
    { 
        #region 成员函数
        public SysAccount CurrentSysAccount { get; set; }
        #endregion

        #region 构造函数
        public SysAccountEditForm(String strUserCode)
        {
            InitializeComponent();
            this.tbUserCode.ReadOnly = true;
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            SysAccount record = new SysAccount();
            record.Id = strUserCode;
            CurrentSysAccount = client.QuerySysAccount(record);
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitControl()
        {
            this.tbUserCode.Text = this.CurrentSysAccount.Id;
            this.tbUserName.Text = this.CurrentSysAccount.UserName;
            this.tbRemark.Text = this.CurrentSysAccount.Remark;
            this.tbsDepartment.SetTextBoxContent(this.CurrentSysAccount.DepartmentName);
            this.tbsDepartment.Tag = this.CurrentSysAccount.DepartmentCode;
            this.tbsWarehouse.SetTextBoxContent(this.CurrentSysAccount.WarehouseName);
            this.tbsWarehouse.Tag = this.CurrentSysAccount.WarehouseCode;
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            SysRole roleRecord = new SysRole();
            roleRecord.Id = this.CurrentSysAccount.UserRole;
            roleRecord = client.QuerySysRole(roleRecord);
            this.tbsUserRole.Tag = this.CurrentSysAccount.UserRole;
            this.tbsUserRole.SetTextBoxContent(roleRecord.RoleName);
            string strItem = string.Empty;
            CommonDefine.DictAccountLock.TryGetValue(this.CurrentSysAccount.IsLocked, out strItem);
            this.cbIsLocked.SelectedItem = strItem;
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.tbUserName.Text.Trim() == "")
            {
                bRet = false;
                this.tbUserName.BackColor = Color.Yellow;

            }
            if (string.IsNullOrWhiteSpace(tbsUserRole.GetTextBoxContent()))
            {
                bRet = false;
                this.tbsUserRole.SetTextBGColor(Color.Yellow);
            }
            //if (string.IsNullOrWhiteSpace(tbsDepartment.GetTextBoxContent()))
            //{
            //    bRet = false;
            //    this.tbsDepartment.SetTextBGColor(Color.Yellow);
            //}
            if (!bRet)
            {
                return;
            }
            if (MessageBox.Show("您确定要更新该用户信息吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                SysAccount record = new SysAccount();
                record.Id = this.tbUserCode.Text.Trim();
                record.UserName = this.tbUserName.Text.Trim();
                record.UserRole = this.tbsUserRole.Tag.ToString();
                record.DepartmentCode = this.tbsDepartment.Tag.ToString(); ;
                record.DepartmentName = this.tbsDepartment.GetTextBoxContent();
                record.WarehouseCode = this.tbsWarehouse.Tag.ToString();
                record.WarehouseName = this.tbsWarehouse.GetTextBoxContent();
                if (this.cbIsLocked.SelectedIndex == 0)
                {
                    record.IsLocked = "1";
                }
                else if (this.cbIsLocked.SelectedIndex == 1)
                {
                    record.IsLocked = "0";
                }
                record.Remark = this.tbRemark.Text.Trim();
                    ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                record.CreateDate = this.CurrentSysAccount.CreateDate;
                record.CreateTime = this.CurrentSysAccount.CreateTime;
                record.Remark = this.tbRemark.Text;
                record.Password = this.CurrentSysAccount.Password;
                ServiceReferenceZwms.ServiceZwmsClient sysClient = new ServiceReferenceZwms.ServiceZwmsClient();
                bRet = sysClient.UpdateSysAccount(record);
                if (bRet)
                {
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("更新失败！");
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void tbsUserRole_SelectButtonClick(object sender, EventArgs e)
        {
            SysRoleWnd uc = new SysRoleWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择角色");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsUserRole.Tag = uc.SelectedRecord.Id;
                tbsUserRole.SetTextBoxContent(uc.SelectedRecord.RoleName);
            }
        }

        private void tbsDepartment_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.DepartmentWnd uc = new InfoWms.DepartmentWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择部门");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsDepartment.Tag = uc.SelectedRecord.Id;
                tbsDepartment.SetTextBoxContent(uc.SelectedRecord.DepartmentName);
            }
        }

        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
            }
        }

        #endregion
    }
}
